﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using ITAP.ShprossesBuilder.Modelling;
using ITAP.ShprossesBuilder.Modelling.Components;
using ITAP.ShprossesBuilder.Manipulation.PopupMenu;

namespace ITAP.ShprossesBuilder.Manipulation
{
    public class CommandPopupMenu : IModelComponentCommand, IModelVisitor
    {
        #region IModelComponentCommand Members

        public List<ITAP.ShprossesBuilder.Modelling.Components.IComponent> ComponentsForProcessing
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion

        #region IModelControlCommand Members

        private MouseManagerEvent mouseEvent;

        public MouseManagerEvent MouseEvent
        {
            get
            {
                return mouseEvent;

            }
            set
            {
                mouseEvent = value;
            }
        }

        protected ModelControl modelControl;
        public ModelControl ModelControl
        {
            get
            {
                return modelControl;
            }
            set
            {
                modelControl = value;
            }
        }

        private Point popupPoint;
        public System.Drawing.Point MouseDownPoint
        {
            get
            {
                return popupPoint;
            }
            set
            {
                popupPoint = value;
            }
        }

        public System.Drawing.Point MouseUpPoint
        {
            get
            {
                return popupPoint;
            }
            set
            {
                popupPoint = value;
            }
        }

        public System.Drawing.Point MouseMovePoint
        {
            get
            {
                return popupPoint;
            }
            set
            {
                popupPoint = value;
            }
        }

        public bool ExecuteImmediately
        {
            get
            {
                return true;
            }
            set
            {
            }
        }

        #endregion

        private List<Type> acceptedComponentTypes;

        private PopupMenuProvider popupProvider;

        public CommandPopupMenu(ModelControl modelControl, Type[] acceptedComponentTypes, PopupMenuProvider popupProvider)
        {
            this.acceptedComponentTypes = new List<Type>(acceptedComponentTypes);
            this.modelControl = modelControl;
            this.popupProvider = popupProvider;
        }

        #region ICommand Members

        public void StartExecute()
        {
            if (modelControl.ModelContainer.SelectedComponentsStack.Count == 0)
                return;

            foreach (IComponent selectedComponent in modelControl.ModelContainer.SelectedComponentsStack)
            {
                if (this.acceptedComponentTypes.Contains(selectedComponent.GetType()))
                {
                    ModelPopupMenu menu = popupProvider.GetMenu(selectedComponent.GetType());
                    
                    if (menu != null)
                        menu.Show(modelControl.PointToScreen(popupPoint));
                    break;
                }
            }
        }

        public void Execute()
        {
        }

        public void EndExecute()
        {
        }

        public bool CanUndo
        {
            get { return false; }
        }

        public bool Undo(ModelControl modelControl)
        {
            return true;
        }

        public bool Redo(ModelControl modelControl)
        {
            return true;
        }

        public bool IsNeedModelUpdate
        {
            get { return false; }
        }

        public string Name
        {
            get { throw new NotImplementedException(); }
        }

        #endregion

        #region ICloneable Members

        public object Clone()
        {
            CommandPopupMenu clone = new CommandPopupMenu(this.modelControl, this.acceptedComponentTypes.ToArray(), popupProvider);
            return clone;

        }

        #endregion

        #region IModelVisitor Members

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.Model model)
        {
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.Connector connector)
        {
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.Shpross shpross)
        {
            throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.Spacer spacer)
        {
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.ShprossConnectorStub stub)
        {
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.ShprossConnector shprossConnector)
        {
            throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.SpacerElement spacerElement)
        {
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.SpacerConnector spacerConnector)
        {
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.ModelCell modelCell)
        {
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.ModelContainer modelContainer)
        {
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.StickConnector stickConnector)
        {
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.SnapOnCap snapOnCap)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(ShprossEndToEndConnector endToEndConnector)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(ShprossChain shprossChain)
        {
        //    throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(SpacerContainer spacerContainer)
        {
            //throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(CrossConnector crossConnector)
        {
       //     throw new NotImplementedException();
        }

        #endregion

        #region ICommand Members


        private bool enabled = true;
        public bool Enabled
        {
            get
            {
                return enabled;
            }
            set
            {
                enabled = value;
            }
        }
        #endregion

        #region ICommand Members


        public bool IsNeedVisitorsUpdate
        {
            get { return false; }
        }

        #endregion
    }
}
